package com.hf.action;

import java.sql.Timestamp;
import java.util.*;

import tool.JsonTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.domain.ClientDetail;
import com.base.domain.Position;
import com.base.service.*;


public class LBSAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String date;
	private String province;
	
	private ImageService is=ImageService.getInstance();
	private List<Position> positions;
	
	public static String logo_1="images/mark_b1.png";
	public static String logo_2="images/mark_b2.png";
	
	private String json_positions;
	
	public LBSAction() {
		// TODO Auto-generated constructor stub
	
	}
	
	public static void main(String[] args){
		LBSAction la=new LBSAction();
		la.execute();
	}
	
	@Override
	public String execute() {
		this.date="2016-4-5";
		
		this.date=(this.date==null || this.date.equals("")?TimeTool.getNowTime().substring(0, 10):this.date);
		String[] arrDate=this.date.split("-");
		List<Image> imgs=this.getImageList(new Integer(arrDate[0]), new Integer(arrDate[1]), new Integer(arrDate[2]));
		
		positions=new ArrayList<Position>();
		for(Image img:imgs){
			if(img.getLatitude()==null || img.getLatitude().equals(""))
				continue;
			//System.out.println(img.getLatitude());
			
			if(img.getType()==1)
				continue;
			
			Position p=new Position();
			float[] xy=new float[2];
			xy[0]=new Float(img.getLatitude());
			xy[1]=new Float(img.getLongitude());
			
			p.setPosition(xy);
			if(img.getType()==1){
				p.setIcon(logo_1);
			}else if(img.getType()==2){
				p.setIcon(logo_2);
			}
			positions.add(p);
		}
		
		try {
			StringBuffer sb=new StringBuffer();
			sb.append("[");
			
			for(int i=0;i<positions.size();i++){
				sb.append("{icon:'");
				sb.append(positions.get(i).getIcon()+"',");
				
				sb.append("position:[");
				sb.append(positions.get(i).getPosition()[1]+","+positions.get(i).getPosition()[0]+"]");
				
				//添加点聚合
				sb.append(",offset: {x: -8,y: -34}");
				
				sb.append("}");
				if(i<positions.size()-1){
					sb.append(",");
				
				}
				
			}
			sb.append("]");
			json_positions=sb.toString();
			
			System.out.println(json_positions);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	

	//获取某日Image数据
	private List<Image> getImageList(int year,int month,int date){
		Timestamp begin=TimeTool.getTimeBeginOrEnd(year, month, date, false);
		Timestamp end=TimeTool.getTimeBeginOrEnd(year, month, date, true);
		String hql="from Image a where a.created>'"+begin+"' and a.created<'"+end+"' order by a.created";	
		List<Image> imgs=	is.getImageDao().findByHql(hql);
		return imgs;
	}
	
	

}
